package com.jin.spirng_boot_learn.controllers;

import com.jin.spirng_boot_learn.constant.Response;
import com.jin.spirng_boot_learn.constant.Source;
import com.jin.spirng_boot_learn.listener.selfdefine.Event;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.event.EventListener;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class EventPublishController {

    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    @GetMapping(value = "/event")
    public Response publishInfo() {
        Event event = new Event(Source.A, "hello event");
        applicationEventPublisher.publishEvent(event);
        return Response.ok;

    }

    /**
     * 使用event listener
     */
    @EventListener(classes = {Event.class})
    public void listen(ApplicationEvent applicationEvent) {

    }

}

